package com.sanjiahantang.youjiaclient.module.common.views.impl;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.annotation.NonNull;
import android.support.v4.app.FragmentManager;
import android.util.Log;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.sanjiahantang.uilibrary.R;
import com.sanjiahantang.uilibrary.drawable.CartDrawable;
import com.sanjiahantang.uilibrary.utils.Utils;
import com.sanjiahantang.youjiaclient.base.BaseMvpActivity;
import com.sanjiahantang.youjiaclient.jpush.JPushBoardCastReceiver;
import com.sanjiahantang.youjiaclient.jpush.JPushUtil;
import com.sanjiahantang.youjiaclient.module.apk.ApkDownloadUtils;
import com.sanjiahantang.youjiaclient.module.common.presenters.MainPresenter;
import com.sanjiahantang.youjiaclient.module.common.views.MainView;
import com.sanjiahantang.youjiaclient.module.decorate.views.impl.DecorateDetailFragment;


import java.util.HashSet;
import java.util.Set;

import cn.jpush.android.api.JPushInterface;

public class MainActivity extends BaseMvpActivity<MainView,MainPresenter> implements MainView,
        RadioGroup.OnCheckedChangeListener {

    private RadioButton rbHome;
    private RadioButton rbCategory;
    private RadioButton rbDecorate;
    private RadioButton rbShoppingCart;
    private RadioButton rbMe;
    private RadioGroup radioGroup;

    public static boolean isForeground = false;


    @Override
    protected void onResume() {
        presenter.onResume($this);
        super.onResume();
    }

    // 点击返回按钮
    @Override
    public void onBackPressed() {
        presenter.onBackPressed();
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        Class fragmentClass = null;
        String tag = null;
        int index = 0;
        boolean flag = false;
        switch (checkedId) {
            case R.id.rbHome:
                tag = TAG_HOME;
                fragmentClass = HomeFragment.class;

                break;
            case R.id.rbDecorate:
                    index = 1;
                    fragmentClass = DecorateDetailFragment.class;
                    tag = TAG_FIT;
                break;
            case R.id.rbCategory:
                index = 2;
                fragmentClass = ShopFragment.class;
                tag = TAG_SHOP;
                break;
            case R.id.rbShoppingCart:
                    index = 3;
                    fragmentClass = ShoppingCartFragment.class;
                    tag = TAG_CART;
                    flag = true;
                break;
            case R.id.rbMe:
                    index = 4;
                    fragmentClass = MeFragment.class;
                    tag = TAG_ME;
                break;
            default:
                break;
        }
        presenter.fragmentSelected($this,fragmentClass,R.id.fragmentRoot,tag,index,flag);

    }


    @Override
    public MainPresenter initPresenter() {
        return new MainPresenter();
    }

    @Override
    public void initView() {
        setContentView(R.layout.activity_main);
        radioGroup = (RadioGroup) findViewById(R.id.radioGroup);
        rbHome = (RadioButton) findViewById(R.id.rbHome);
        rbCategory = (RadioButton) findViewById(R.id.rbCategory);
        rbShoppingCart = (RadioButton) findViewById(R.id.rbShoppingCart);
        rbDecorate = (RadioButton) findViewById(R.id.rbDecorate);
        rbMe = (RadioButton) findViewById(R.id.rbMe);

        Log.e("jpush-appkey",JPushUtil.getAppKey(getApplicationContext()));


        Set<String> set = new HashSet<>();
        set.add("0");
        JPushInterface.setTags($this,0,set);


    }

    @Override
    public void initData() {
        ApkDownloadUtils.versionCheck(this, 0);
        presenter.onLoad($this);
        presenter.initFragment(R.id.fragmentRoot,TAG_HOME);
        presenter.registerBroadcastReceiver($this);
        dealBottomButtonsClickEvent();
        presenter.getPersonalInfo($this);
        int page = getIntent().getIntExtra(KEY_PAGE,0);
        if (page > 0){
            setFragmentIndex(page);
        }
    }

    /**
     * 处理底部点击事件
     */
    @Override
    public void dealBottomButtonsClickEvent() {
        radioGroup.setOnCheckedChangeListener(this);
    }

    @Override
    public void close() {
        $this.finish();
    }





    public void setFragmentIndex(int index){
        switch (index){
            case 0:
                rbHome.setChecked(true);
                radioGroup.check(R.id.rbHome);
                break;

            case 1:
                rbDecorate.setChecked(true);
                radioGroup.check(R.id.rbDecorate);
                break;
            case 2:
                rbCategory.setChecked(true);
                radioGroup.check(R.id.rbCategory);
                break;
            case 3:
                rbShoppingCart.setChecked(true);
                radioGroup.check(R.id.rbShoppingCart);
                break;
            case 4:
                rbMe.setChecked(true);
                radioGroup.check(R.id.rbMe);
                break;
        }

    }

    @Override
    public void startFragment(int i){
        Class fragment = null;
        int rootId = R.id.fragmentRoot;
        String tag = null;
        switch (i){
            case 0:
                fragment = HomeFragment.class;
                tag = TAG_HOME;
                break;
            case 1:
                fragment = DecorateDetailFragment.class;
                tag = TAG_FIT;
                break;
            case 2:
                fragment = ShopFragment.class;
                tag = TAG_SHOP;
                break;
            case 3:
                fragment = ShoppingCartFragment.class;
                tag = TAG_CART;
                break;
            case 4:
                fragment = MeFragment.class;
                tag = TAG_ME;
                break;
        }
        presenter.startFragment(fragment,rootId,tag);
    }

    @Override
    public CartDrawable getCartDrawable(int i) {
        return (CartDrawable) rbShoppingCart.getCompoundDrawables()[i];
    }

    @Override
    public boolean isFirstPage() {
        return presenter.isFirstPage($this,rbHome,TAG_HOME);
    }

    @Override
    public FragmentManager getFManager() {
        return getSupportFragmentManager();
    }


    @Override
    public void setButtonImage(CartDrawable cartDrawable) {
        rbShoppingCart.setCompoundDrawablesWithIntrinsicBounds(null, cartDrawable, null, null);
    }

    @Override
    public void showProgress() {
        showProgress($this);
    }

    @Override
    public void hideProgress() {
        super.hideProgress();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        getSupportFragmentManager().findFragmentById(R.id.fragmentRoot).onActivityResult(requestCode,resultCode,data);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        getSupportFragmentManager().findFragmentById(R.id.fragmentRoot).onRequestPermissionsResult(requestCode,permissions,grantResults);
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }




    //for receive customer msg from jpush server
    private MessageReceiver mMessageReceiver;
    public static final String MESSAGE_RECEIVED_ACTION = "com.sanjiahantang.youjiaclient.MESSAGE_RECEIVED_ACTION";
    public static final String KEY_TITLE = "title";
    public static final String KEY_MESSAGE = "message";
    public static final String KEY_EXTRAS = "extras";

    public void registerMessageReceiver() {
        mMessageReceiver = new MessageReceiver();
        IntentFilter filter = new IntentFilter();
        filter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
        filter.addAction(MESSAGE_RECEIVED_ACTION);
        JPushBoardCastReceiver.getInstance(this).registerReceiver(mMessageReceiver, filter);
    }

    public class MessageReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            try {
                if (MESSAGE_RECEIVED_ACTION.equals(intent.getAction())) {
                    String messge = intent.getStringExtra(KEY_MESSAGE);
                    String extras = intent.getStringExtra(KEY_EXTRAS);
                    StringBuilder showMsg = new StringBuilder();
                    showMsg.append(KEY_MESSAGE + " : " + messge + "\n");
                    if (!JPushUtil.isEmpty(extras)) {
                        showMsg.append(KEY_EXTRAS + " : " + extras + "\n");
                    }
                    setCostomMsg(showMsg.toString());
                }
            } catch (Exception e){
            }
        }
    }

    private void setCostomMsg(String msg){
        Utils.showToast($this,msg);
    }
}
